package servlet;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import util.*;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

public class SearchByName extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int n = 0;
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        JSONObject jsonObject=new JSONObject();
        JSONObject jsonObject1=new JSONObject();
        JSONArray result = new JSONArray();
        jsonObject.put("status",200);
        jsonObject.put("message","");

        List<staff> list =new ArrayList<staff>();
        request.setCharacterEncoding("utf-8");
     //   String name=request.getParameter("name").trim();//取得用户名参数

        String sql="select * from staff ";
        try {
            DButil dButil = new DButil();
            Connection con = dButil.getConnection();

            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);

            while(rs.next()) {
                staff staff = new staff();
                staff.setId(rs.getString("id"));
                staff.setName(rs.getString("name"));
                staff.setWage(rs.getDouble("wage"));
                staff.setWyear(rs.getDouble("wyear"));
                staff.setTel(rs.getString("tel"));
                staff.setAddress(rs.getString("address"));
                staff.setOrigin(rs.getString("origin"));
                staff.setDegree(rs.getString("degree"));
                staff.setBirth(rs.getString("birth"));
                staff.setSex(rs.getString("sex"));

                list.add(staff);
                n++;
            }

            result = JSONArray.fromObject( list);
            jsonObject.put("total",n);

/*

            //将数据库中查询到的信息封装在user对象中，use对象保存在request中，之后将通过request对象将数据传递到页面
            //需要的话也可以只传递一个参数request.setAttribute("id", user.getId());
            request.setAttribute("List", list);
            //这里是转发，从Servlet跳转到showinfo.jsp页面，并且带上request和response对象中原有的参数
            request.getRequestDispatcher("showselect.jsp").forward(request, response);
*/
        } catch (SQLException e) {

        }

        jsonObject1.put("item",result);
        jsonObject.put("rows",jsonObject1);
        System.out.println(jsonObject.toString());
        out.println(jsonObject.toString());
        out.flush();
        out.close();

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}